import { action, observable } from 'mobx';
import { message, Modal } from 'antd';
import * as API from '../../apis/performance/scoreSettingApi';

/**
 * 绩效-考核分数段
 * jiansong 2019/6/11
 */
export default class ScoreSettingStore {

    @observable loading = true; //加载中
    @observable showUpdataDialog = false; //是否展示新增或编辑对话框
    @observable data = []; //表单数据
    @observable selectedRowKeys = []; //选中的行
    @observable canedit = false; //是否能编辑

    @observable form = { //表单数据
        operation: "", //操作类型 add、edit、del
        gardename: "", //分段名称
        beginSymbol: "1", //开始值符号
        beginscore: "", //开始值
        endSymbol: "4", //结束值符号
        endscore: "", //结束值
        rank: "", //显示顺序
    };

    symbolValue = ["大于", "大于等于", "小于", "小于等于"];
    symbolOption = [
        { key: "1", showname: "大于" },
        { key: "2", showname: "大于等于" },
        { key: "3", showname: "小于" },
        { key: "4", showname: "小于等于" }
    ];

    @action("获取table数据")
    getTableDatas = (params) => {
        this.loading = true;
        API.socreSettingApi(params).then(action(res => {
            if (res.api_status) { // 接口请求成功
                this.data = res.datas; // table 请求数据
                this.canedit = res.canedit;
            } else {
                message.error(res.msg || '接口调用失败！')
            }
            this.loading = false;
        }));
    };

    @action("切换编辑框是否展示")
    changeShowUpdataDialog = () => {
        this.showUpdataDialog = !this.showUpdataDialog
    };

    @action("表单关闭的时候清空表单内容")
    closeForm = () => {
        this.initForm()
        this.changeShowUpdataDialog()
    };

    @action("搜索条件改变")
    onFormChange = (params) => {
        for (let key in params) {
            this.form[key] = params[key];
        }
    }

    @action("初始化添加表单")
    ininAddForm = () => {
        this.form.operation = "add"
        this.changeShowUpdataDialog()
    };

    @action("初始化表单内容")
    initForm = () => {
        this.form = {
            operation: "", //操作类型
            gardename: "", //分段名称
            beginSymbol: "1", //开始值符号
            beginscore: "", //开始值
            endSymbol: "4", //结束值符号
            endscore: "", //结束值
            rank: "", //显示顺序
        }
    }

    @action("初始化编辑表单内容")
    initEditForm = (params) => {
        this.form = {
            operation: "edit",
            ...params
        }
        this.changeShowUpdataDialog()
    }

    @action("考核指标项-操作")
    operation = () => {
        if (this.checkParams()) {
            this.loading = true;
            API.socreSettingOperationApi(this.form).then(action(res => {
                if (res.api_status) {
                    if (!res.hasRight) {
                        message.error("对不起，您暂时没有权限！");
                    } else {
                        message.success("操作成功");
                        this.changeShowUpdataDialog()
                        this.getTableDatas()
                        this.initForm()
                    }
                } else {
                    message.error(res.msg || '接口调用失败！')
                }
                this.loading = false;
            }));
        }
    }

    //校验表单参数
    checkParams = () => {
        if (this.form.gardename === '') {
            message.error("分段名称未填写")
            return false;
        }
        if (this.form.beginscore === '') {
            message.error("开始值未填写")
            return false;
        }
        if (this.form.endscore === '') {
            message.error("结束值未填写")
            return false;
        }
        return true;
    }

    @action("考核指标项-操作删除")
    operationDel = () => {
        if (this.selectedRowKeys.length > 0) {
            Modal.confirm({
                title: '信息确认',
                content: '确定删除已选数据?',
                onOk: () => {
                    let params = {
                        ids: this.selectedRowKeys.join(","),
                        operation: "del",
                    }
                    this.loading = true;
                    API.socreSettingOperationApi(params).then(action(res => {
                        if (res.api_status) {
                            if (!res.hasRight) {
                                message.error("对不起，您暂时没有权限！");
                            } else {
                                this.selectedRowKeys = [];
                                message.success("操作成功");
                                this.getTableDatas();
                            }
                        } else {
                            message.error(res.msg || '接口调用失败！')
                        }
                        this.loading = false;
                    }));

                },
            });
        } else {
            Modal.warning({
                title: '系统信息',
                content: '请选择要删除的数据!'
            });
        }

    }

}